package com.course.utils;

import io.netty.util.internal.StringUtil;

import javax.servlet.http.HttpServletRequest;

public class CommonUtil {

    /**
     * 取得request中参数值--字符型
     * @param request
     * @param paramName
     * @return
     */
    public static final String getParam(HttpServletRequest request,String paramName, String defaultValue){
        String value = null;
        if(request.getParameter(paramName)!=null&&!request.getParameter(paramName).trim().equals("")){
            value = request.getParameter(paramName).trim();
        }else{
            value = defaultValue;
        }
        return value;
    }

    /**
     * 取得request中参数值--boolean型
     * @param request
     * @param paramName
     * @param flag
     * @return
     */
    public static final boolean getParam(HttpServletRequest request, String paramName, boolean flag){
        if(request.getParameter(paramName) != null &&
                "true".equalsIgnoreCase(request.getParameter(paramName).trim())){
            return true;
        }
        return flag;
    }

    /**
     * 取得request中参数值--整形
     * @param request
     * @param paramName
     * @return
     */
    public static final Integer getParam(HttpServletRequest request,String paramName,Integer defaultValue){
        String value = null;
        if(request.getParameter(paramName)!=null && !request.getParameter(paramName).trim().equals("") && !request.getParameter(paramName).trim().equals("null")){
            value = request.getParameter(paramName).trim();
            return Integer.parseInt(value);
        }else{
            return defaultValue;
        }
    }
}
